package com.lml.jdk8.localdate;

import org.junit.jupiter.api.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2021/6/24 16:07
 * Description:
 */
public class LocalDateTest {

    @Test
    public void test01() {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int monthValue = now.getMonthValue();
        int dayOfMonth = now.getDayOfMonth();
        System.out.println(year + "年" + monthValue + "月" + dayOfMonth + "月");

    }

    @Test
    public void test02() {
        LocalDate date = LocalDate.of(2020, 3, 10);
        LocalDate now = LocalDate.now();
        System.out.println(date);
        System.out.println(date.equals(now));
        System.out.println(date.isBefore(now));
    }

    @Test
    public void test03() {
//        LocalDate today = LocalDate.now();
        LocalDate today = LocalDate.of(2021, 4, 4);
        LocalDate dayOfBirth = LocalDate.of(1995, 4, 4);
        MonthDay birthDay = MonthDay.of(dayOfBirth.getMonth(), dayOfBirth.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(today);
        if (birthDay.equals(currentMonthDay)) {
//        if (birthDay.compareTo(currentMonthDay) == 0) {
            System.out.println("Today is my birthday!!");
        } else {
            System.out.println("Today is NOT my birthday");
        }
    }

    @Test
    public void test04() {
        LocalTime now = LocalTime.now();
        LocalTime localTime = now.plusHours(2);
        System.out.println("Time after 2 hours: " + localTime);
    }

    @Test
    public void test05() {
        LocalDate now = LocalDate.now();
        LocalDate nextYear = now.plusYears(1);
        System.out.println("1年后：" + nextYear);

        LocalDate previousYear = now.minusYears(1);
        System.out.println("1年前：" + previousYear);
    }

    @Test
    public void test06() {
        System.out.println("今年是否是闰年：" + LocalDate.now().isLeapYear());
        System.out.println("当前的时间戳：" + Instant.now());
    }

    //LocalDate与String的相互转换
    @Test
    public void test07() {
        // String -> LocalDate
//        LocalDate parse = LocalDate.parse("20210101", DateTimeFormatter.BASIC_ISO_DATE);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate parse = LocalDate.parse("2021-01-01", formatter);
        System.out.println("parse = " + parse);
        //LocalDate -> String
        String format = LocalDate.now().format(formatter);
//        String format = formatter.format(LocalDate.now());
        System.out.println("format = " + format);
    }

    //LocalDateTime与Date相互转换
    @Test
    public void test08() {
        //Date -> LocalDateTime
        Date date = new Date();
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        System.out.println("localDate = " + localDate);

        //LocalDateTime -> Date
        Instant instant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();
        Date from = Date.from(instant);
        System.out.println("from = " + from);
    }

    //找出某一年内的所有周六日，按月份分组
    public Map<YearMonth, List<String>> findWeekEndDaysOfYear(int year) {
        return null;
    }

    //找出某月份有多少个周六日
    public List<String> findWeekEndDaysOfMonth(YearMonth yearMonth) {
        return null;
    }
}
